package com.yf1706.service;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yf1706.bean.CountOvertimeSalaryRate;
import com.yf1706.dao.jpa.ICountOvertimeSalaryRateJDAO;

/**
 * 加班费率
 * 
 * @author 锦涛
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CountOvertimeSalaryRateService {

	@Autowired
	private ICountOvertimeSalaryRateJDAO cOvertimeSalaryJDAO;

	// 增加加班费率
	@Transactional
	public void save(CountOvertimeSalaryRate cOvertimeSalary) {
		cOvertimeSalaryJDAO.save(cOvertimeSalary);
	}

	// 根据ID删除加班费率
	@Transactional
	public void remove(String ids) {
		for (String id : ids.split(",")) {
			if (id != null) {
				cOvertimeSalaryJDAO.deleteById(Integer.parseInt(id));
			}
		}
	}

	/**
	 * 修改数据
	 * 
	 * @param cRate 先根据要修改的对象id查询出对应的持久化对象 直接调用持久化对象的set方法修改对象的数据
	 */
	@Transactional
	public void update(CountOvertimeSalaryRate cOvertimeSalary) {
		Optional<CountOvertimeSalaryRate> sessionCOverSalary = cOvertimeSalaryJDAO.findById(cOvertimeSalary.getOvertimeSalaryRateId());
		sessionCOverSalary.get().setOvertimeSalaryRateName(cOvertimeSalary.getOvertimeSalaryRateName());
		sessionCOverSalary.get().setRate(cOvertimeSalary.getRate());
		sessionCOverSalary.get().setEverydayWorktime(cOvertimeSalary.getEverydayWorktime());
	}

	// 分页查询加班费率
	@Transactional
	public Page<CountOvertimeSalaryRate> querybypage(int page, int rows, String sort, String order) {
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page - 1, rows, Sort.Direction.DESC, sort);
			return cOvertimeSalaryJDAO.findAll(pageable);
		} else {
			Pageable pageable = PageRequest.of(page - 1, rows, Sort.Direction.ASC, sort);
			return cOvertimeSalaryJDAO.findAll(pageable);
		}
	}
}
